package gdx_tyrian.screen;

import gdx_tyrian.Tyrian;

import javax.sound.midi.Sequence;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Scaling;

public class SplashScreen extends AbstractScreen {
	private Texture splashTexture;
	private TextureRegion splashTextureRegion;

	public SplashScreen(Tyrian game) {
		super(game);
	}

	@Override
	public void show() {
		super.show();

		// load the splash image and create the texture region
		splashTexture = new Texture("splash.png");

		// we set the linear texture filter to improve the stretching
		splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

		// in the image atlas, our splash image begins at (0,0) at the
		// upper-left corner and has a dimension of 512x301
		splashTextureRegion = new TextureRegion(splashTexture, 0, 0, 512, 301);
		
	}

	@Override
	public void render(float delta) {
		super.render(delta);

		// // we use the SpriteBatch to draw 2D textures (it is defined in our
		// base
		// // class: AbstractScreen)
		// batch.begin();
		//
		// // we tell the batch to draw the region starting at (0,0) of the
		// // lower-left corner with the size of the screen
		// batch.draw(splashTextureRegion, 0, 0, Gdx.graphics.getWidth(),
		// Gdx.graphics.getHeight());
		//
		// // the end method does the drawing
		// batch.end();

	}

	@Override
	public void resize(int width, int height) {
		// let's make sure the stage is clear
		stage.clear();

		// here we create the splash image actor and set its size
		TextureRegionDrawable splashDrawable = new TextureRegionDrawable(
				splashTextureRegion);
		Image splashImage = new Image(splashDrawable, Scaling.stretch,
				Align.bottom | Align.left);
		splashImage.setWidth(width);
		splashImage.setHeight(height);

		// this is needed for the fade-in effect to work correctly; we're just
		// making the image completely transparent
		splashImage.getColor().a = 0.0f;

		// configure the fade-in/out effect on the splash image
		SequenceAction seq = new SequenceAction();
		seq.addAction(Actions.fadeIn(0.75f));
		seq.addAction(Actions.delay(1.75f));
		seq.addAction(Actions.fadeOut(0.75f));
		
		Action nextScreen = new Action() {
			public boolean act(float delta) {
				// game.setScreen( new MenuScreen( game ) );
				return true; // returning true consumes the event
			}
		};
		seq.addAction(nextScreen);
		splashImage.addAction(seq);

		// and finally we add the actors to the stage, on the correct order
		stage.addActor(splashImage);
	}

	@Override
	public void dispose() {
		super.dispose();
		splashTexture.dispose();
	}
}